package paulsnet;

import com.jme3.app.SimpleApplication;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.network.serializing.Serializer;
import com.jme3.system.JmeContext;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import com.simsilica.es.base.DefaultEntityData;
import com.simsilica.es.net.EntitySerializers;
import com.simsilica.es.server.EntityDataHostService;

/**
 *
 * @author eike
 */
public class ServerSlow extends SimpleApplication {

    EntityData ed;
    EntityDataHostService edhs;
    float tpc, tpu = 0.05f;
    long lastTime;
    EntityId eId, eId2;

    public static void main(String[] args) {
        ServerSlow m = new ServerSlow();

        m.start(JmeContext.Type.Headless);
    }

    @Override
    public void simpleInitApp() {
        ed = new DefaultEntityData();
        eId = ed.createEntity();
        eId2 = ed.createEntity();
        EntitySerializers.initialize();
        Serializer.registerClass(TestComponent.class);
        try {
            Server s = Network.createServer(1235);
            s.addChannel(1236);
            s.start();
            edhs = new EntityDataHostService(s, 0, ed);
            edhs.setAutoHost(true);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void simpleUpdate(float tpf) {
//        eId = ed.createEntity(); //With this it is fast, also.
        long now = System.nanoTime();
        long trueTpf = now - lastTime;
        lastTime = now;
        System.out.println(trueTpf / 1E9);
//        System.out.println(tpf);
        tpc += tpf;
        if (tpc > tpu) {
            System.out.println(tpc);
            tpc = 0;
//            ed.setComponent(eId, new TestComponent());
//            ed.setComponent(eId, new TestComponent());
//            ed.setComponent(eId, new TestComponent());
//            ed.setComponent(eId, new TestComponent());
            ed.setComponent(eId, new TestComponent());
//            ed.setComponent(eId2, new TestComponent());
            edhs.sendUpdates();
        }
    }
}
